package com.hctforgreen.greenservice;

import java.io.File;
import java.io.IOException;

import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.utils.FileCacheUtil;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.SelectStoreUtil;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;
import com.hctforgreen.greenservice.utils.Utils;

/**
 * 下载路径设置
 * 
 * @author denghb
 * 
 */
public class DownloadStoreSettingActivity extends ParentActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_download_setting);
		initSkinLayout();
		initTitleButtonBar();
		initWindow();
	}

//	private void initSkinLayout() {
//		LinearLayout parentLyt = (LinearLayout) findViewById(R.id.lyt_parent);
//		parentLyt.setBackgroundResource(ThemeStoreUtil.getSkinId(this));
//	}

	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.download_stroe_setting_name));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void initWindow() {

		final CheckBox cbInner = (CheckBox) findViewById(R.id.cb_inner);
		final CheckBox cbOutter = (CheckBox) findViewById(R.id.cb_outter);
		if (0 == SelectStoreUtil.get(getApplicationContext())) {
			cbInner.setChecked(true);
			cbOutter.setChecked(false);
		} else {
			cbInner.setChecked(false);
			cbOutter.setChecked(true);
		}
		final String[] sdCards = Utils.getSDCardPathEx(this);
		try {
			if (sdCards == null || sdCards.length < 2) {
				SelectStoreUtil.save(this, 0);
				cbInner.setChecked(true);
				cbOutter.setChecked(false);

				findViewById(R.id.lyt_download_outter_sd).setVisibility(
						View.GONE);
				findViewById(R.id.lyt_download_inner_sd).setBackgroundResource(
						R.drawable.btn_setting_single_item_layout);
			}
		} catch (Exception e) {
			SelectStoreUtil.save(this, 0);
			cbInner.setChecked(true);
			cbOutter.setChecked(false);
			findViewById(R.id.lyt_download_outter_sd).setVisibility(View.GONE);
			e.printStackTrace();
		}
		final View pbInner = findViewById(R.id.inner_loading);
		final View innerSd = findViewById(R.id.lyt_download_inner_sd);
		final View pbOutter = findViewById(R.id.outter_loading);
		final View outterSd = findViewById(R.id.lyt_download_outter_sd);
		innerSd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (!cbInner.isChecked()) {

					final Handler handler = new Handler() {
						public void handleMessage(android.os.Message msg) {
							if (msg.what == HctConstants.STATUS_SUCCESS) {
								SelectStoreUtil
										.save(getApplicationContext(), 0);
								outterSd.setClickable(true);
								innerSd.setClickable(true);
								cbInner.setChecked(true);
								cbOutter.setChecked(false);
								pbInner.setVisibility(View.GONE);
								Toast.makeText(
										getApplicationContext(),
										getString(R.string.download_store_set_success),
										Toast.LENGTH_LONG).show();
							}
						};
					};
					pbInner.setVisibility(View.VISIBLE);
					outterSd.setClickable(false);
					innerSd.setClickable(false);
					new HandlerThread("") {
						@Override
						public void run() {
							try {
								if (sdCards != null && sdCards.length > 1) {
									FileCacheUtil
											.copy(new File(
													sdCards[1]
															+ HctConstants.BOOKS_CACHE_STORE_PATH),
													new File(sdCards[0]));
									FileCacheUtil
											.copy(new File(
													sdCards[1]
															+ HctConstants.DEFAULT_VIDEO_STORE_PATH),
													new File(
															sdCards[0]
																	+ HctConstants.DEFAULT_VIDEO_STORE_EXTERNEL_PATH));
								}
								handler.sendEmptyMessage(HctConstants.STATUS_SUCCESS);
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}.start();

				}
			}
		});

		outterSd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!cbOutter.isChecked()) {

					final Handler handler = new Handler() {
						public void handleMessage(android.os.Message msg) {
							if (msg.what == HctConstants.STATUS_SUCCESS) {
								SelectStoreUtil
										.save(getApplicationContext(), 1);
								outterSd.setClickable(true);
								innerSd.setClickable(true);
								cbOutter.setChecked(true);
								cbInner.setChecked(false);
								pbOutter.setVisibility(View.GONE);
								Toast.makeText(
										getApplicationContext(),
										getString(R.string.download_store_set_success),
										Toast.LENGTH_LONG).show();
							}
						};
					};
					pbOutter.setVisibility(View.VISIBLE);
					outterSd.setClickable(false);
					innerSd.setClickable(false);
					new HandlerThread("") {
						@Override
						public void run() {
							try {
								if (sdCards != null && sdCards.length > 1) {
									FileCacheUtil
											.copy(new File(
													sdCards[0]
															+ HctConstants.BOOKS_CACHE_STORE_PATH),
													new File(sdCards[1]));

									FileCacheUtil
											.copy(new File(
													sdCards[0]
															+ HctConstants.DEFAULT_VIDEO_STORE_PATH),
													new File(
															sdCards[1]
																	+ HctConstants.DEFAULT_VIDEO_STORE_EXTERNEL_PATH));
								}
								handler.sendEmptyMessage(HctConstants.STATUS_SUCCESS);
							} catch (IOException e) {
								e.printStackTrace();
							}
						}
					}.start();
				}
			}
		});
	}
}
